<!--
 * @Author: 杨光辉(GerhardYang)
 * @Date: 2021-06-07 10:46:19
 * @LastEditors: 杨光辉(GerhardYang)
 * @LastEditTime: 2021-09-26 02:01:43
 * @Description: file content
 * @Copyright: 超图软件华中平台客户中心 (SuperMap Software Co., Ltd. -Central China Platform)
-->
<template>
  <a-form layout="inline" style="margin-top: 6px">
    <a-form-item>
      <a-select
        size="small"
        v-model="selectedField"
        :disabled="disabled"
        style="width: 170px"
      >
        <a-select-option
          placeholder="选择查询字段"
          v-for="(item, index) in searchField"
          :key="index"
          :value="item.title"
        >
          {{ item.title }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item>
      <a-select
        size="small"
        style="width: 70px"
        v-model="selectedMode"
        :disabled="disabled"
      >
        <a-select-option
          v-for="(item, index) in searchMode"
          :key="index"
          :value="item"
        >
          {{ item }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item>
      <a-input
        v-model="searchValue"
        :disabled="disabled"
        size="small"
        style="width: 170px"
        placeholder="查询条件"
      />
    </a-form-item>
    <a-form-item>
      <a-button
        :disabled="disabled"
        size="small"
        type="primary"
        style="width: 70px"
        @click="onSearch"
        >搜索</a-button
      >
    </a-form-item>
    <SearchResultByClass ref="SearchResultByClass" />
  </a-form>
</template>

<script>
import {
  dataGetFeaturesBySQLParameters,
  dataGetFeaturesBySQL,
} from "@/utils/http/iserverQuery";
import { showBottomPanel } from "@/utils/divResize";
import localStorage from "@/utils/localStorage";
import SearchResultByClass from "./SearchResultByClass";
export default {
  data() {
    return {
      selectedField: "选择查询字段",
      selectedMode: "LIKE",
      searchMode: ["=", ">", ">=", "<", "<=", "LIKE"],
      searchValue: "",
      disabled: true,
      searchField: [],
      SearchDictionary: JSON.parse(localStorage.get("SearchDictionary")),
      tagitem: {},
    };
  },
  components: {
    SearchResultByClass,
  },

  methods: {
    onClose() {
      this.selectedField = "选择查询字段";
      this.searchValue = "";
      this.disabled = true;
      this.searchField = [];
      this.$refs.SearchResultByClass.onClose();
    },
    async getSearchField() {
      this.searchField = [];
      //构建查询参数
      let sql = "SMID = 1";
      let datasetNames = [`${this.tagitem.datasource}:${this.tagitem.dataset}`];
      let params = dataGetFeaturesBySQLParameters(datasetNames, sql);

      const dataUrl = window.getServiceUrl(
        this.tagitem.dadaUrl,
        window.servicesPorts.iserver
      );
      //查询第一条数据，获取查询字段
      let result = await dataGetFeaturesBySQL(dataUrl, params);
      let properties = result.features.features[0].properties;
      //构建显示字段
      for (var item in properties) {
        //屏蔽系统字段
        if (!item.startsWith("SM") || !item.startsWith("Sm")) {
          //匹配中文
          for (let dict of this.SearchDictionary) {
            if (dict.ename.toUpperCase() == item.toUpperCase()) {
              let str =
                '{"title":"' + dict.cname + '","dataIndex":"' + item + '"}';
              let obj = JSON.parse(str);
              this.searchField.push(obj);
            }
          }
        }
      }
      this.disabled = false;
    },
    onSearch() {
      if (this.selectedField == "选择查询字段") {
        this.$message.warning("请先选择查询字段");
        return;
      }
      const field = this.searchField.filter(
        (item) => item.title == this.selectedField
      )[0].dataIndex;
      if (this.selectedMode === "LIKE") {
        const sql = `${field} ${this.selectedMode} '%${this.searchValue}%'`;
        this.sqlQuery(sql);
      } else {
        const sql = `${field} ${this.selectedMode} '${this.searchValue}'`;
        this.sqlQuery(sql);
      }
    },
    async sqlQuery(sql) {
      //构建查询参数
      let datasetNames = [`${this.tagitem.datasource}:${this.tagitem.dataset}`];
      let params = dataGetFeaturesBySQLParameters(datasetNames, sql);

      const dataUrl = window.getServiceUrl(
        this.tagitem.dadaUrl,
        window.servicesPorts.iserver
      );

      showBottomPanel();
      this.$refs.SearchResultByClass.visible = true;
      this.$refs.SearchResultByClass.loading = true;
      let result = await dataGetFeaturesBySQL(dataUrl, params);

      //判断是否匹配到查询
      let quretResultRecordset;
      if (result.features.features.length > 0) {
        quretResultRecordset = {
          key: this.tagitem.title,
          datasetNames: datasetNames,
          dataUrl: dataUrl,
          result: result,
        };
        //构建搜索结果面板数据
        console.log("result", result);
        console.log("quretResultRecordset", quretResultRecordset);
        this.$refs.SearchResultByClass.buildResultData(quretResultRecordset);
      } else {
        this.$message.warning("没有查询到数据");
        // 延时关闭，避免快速关闭窗口，页面闪调
        setTimeout(() => {
          this.$refs.SearchResultByClass.onClose();
        }, 1000);
        return;
      }
    },
  },
};
</script>
